﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace DuelQuizzServer
{
    public partial class Friend
    {
        private static Entities context;
        private static DbSet<Friend> entities
        {
            get
            {
                if (Friend.context == null)
                {
                    Friend.context = new Entities();
                    Friend.context.Configuration.ProxyCreationEnabled = false;
                    Friend.context.Configuration.LazyLoadingEnabled = true;
                }
                return context.Friend;
            }
        }

        internal static List<User> getFriend(int user_id){
            List<User> list_user = new List<User>();
            try
            {
                List<Friend> list_friend = entities.Where(p => (p.user_id == user_id)).ToList();
                if (list_friend != null)
                {
                    foreach (Friend friend in list_friend)
                    {
                        list_user.Add(friend.ToUser());
                    }
                }
            }
            catch (Exception e)
            {

            }

            return list_user;
        }

        internal User ToUser() {
            return User.find(friend_id,false);
        }

        internal static bool addFriend(int friend_id,int user_id)
        {
            try
            {
                Friend f = new Friend();
                f.friend_id = friend_id;
                f.user_id = user_id;
                f.state = "yup";
                entities.Add(f);

                context.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

    }
}